/*
 * JadwalManager.java
 *
 * Created on April 18, 2007, 12:18 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.wii.jadwal.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author Hendro
 */
public class JadwalManager {
    
    /** Creates a new instance of JadwalManager */
    public JadwalManager() {
    }
    
    public void insertJadwal(Jadwal jadwal) throws Exception{
        Session session = HibernateUtil.getCurrentSession();
        session.beginTransaction();
        try{
            session.save(jadwal);
            session.getTransaction().commit();
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
    }
    
    public Jadwal loadJadwal(long id) throws Exception{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Jadwal jadwal = null;
        try{
            jadwal = (Jadwal) session.load(Jadwal.class,id);
            session.getTransaction().commit();
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
        return jadwal;
    }
    
    public List listAllJadwal() throws Exception{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = new ArrayList();
        try{
            list = session.createQuery("FROM Jadwal").list();
            session.getTransaction().commit();
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
        return list;
    }
    
    public List listAllJadwalHariIni() throws Exception{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = new ArrayList();
        try{
            list = session.createQuery("SELECT jadwal FROM Jadwal jadwal WHERE jadwal.warningDate= :tgl ORDER BY id ASC")
            .setParameter("tgl",new Date()).list();
            session.getTransaction().commit();
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
        return list;
    }
    
    public void updateJadwal(Jadwal jadwal) throws Exception{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try{
            session.update(jadwal);
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
    }
    
    public List getListJadwalByKeyWord(String keyword) throws Exception{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = new ArrayList();
        try{
            list = session.createQuery("SELECT jadwal FROM Jadwal jadwal WHERE jadwal.keyword LIKE '%"+keyword+"%'")
            .list();
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
        return list;
    }
    
    public List getListJadwalByDate(Date awal,Date akhir) throws Exception{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = new ArrayList();
        try{
            list = session.createQuery("SELECT jadwal FROM Jadwal jadwal WHERE jadwal.warningDate>= :input1 AND jadwal.warningDate<= :input2")
            .setParameter("input1",awal).setParameter("input2",akhir).list();
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
        return list;
    }
    
    public void deleteJadwal(Jadwal jadwal) throws Exception{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try{
            session.delete(jadwal);
        }catch(Exception ex){
            session.getTransaction().rollback();
            throw ex;
        }
    }
}
